<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <script type="text/javascript" src="/static/js/jquery.min.js"></script>
    <script type="text/javascript" src="/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <link rel="stylesheet" href="/static/sweetalert/sweetalert.css">
    <script src="/static/sweetalert/sweetalert.min.js"></script>
    <script src="/static/sweetalert/ions_alert.js"></script>
</head>
  
  <body>
    <div class="x-body">
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">上级权限</label>
                <div class="layui-input-inline"  style="width: 200px;">
                    <select lay-filter="aihao" id="auth_parent_id">
                        <option value="0">--无--</option>
                        {{range .auths}}
                            <option value="{{.Id}}">{{.AuthName}}</option>
                        {{end}}
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">无上级权限请留空</div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">权限名称</label>
                <div class="layui-input-inline" style="width:200px;">
                    <input type="text" lay-verify="title" placeholder="请输入权限名称" autocomplete="off" class="layui-input" id="auth_name">
                </div>
                <div class="layui-form-mid layui-word-aux">权限名称不能重复</div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label">URL地址</label>
                <div class="layui-input-inline" style="width:200px;">
                    <input type="text" id="auth_url" lay-verify="title" placeholder="请输入URL地址" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">使用urlfor规则</div>
            </div>





            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">描述</label>
                <div class="layui-input-block">
                    <textarea id="auth_desc" placeholder="请输入内容" class="layui-textarea"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">是否启用</label>
                <div class="layui-input-block">
                    <input name="is_active" id="is_active_1" type="radio" value="1" checked="checked" title="启用"/>
                    <input name="is_active" id="is_active_0" type="radio" value="0" title="停用"/>
                </div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label">权重</label>
                <div class="layui-input-inline" style="width:200px;">
                    <input type="text" id="auth_weight" lay-verify="title" placeholder="请输入数字权重" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">权重只能为数字,值越大排序越前</div>
            </div>


            <div class="layui-form-item">
                <div class="layui-input-block">
                    <div class="layui-footer" >
                        <button lay-filter="add" lay-submit class="layui-btn">立即提交</button>
                        <a href="" class="layui-btn layui-btn-primary">返回</a>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <script>
        $(function  () {
            layui.use('form', function () {
                var form = layui.form;

                //监听提交
                form.on('submit(add)', function (data) {
                    var auth_parent_id = document.getElementById("auth_parent_id").value;
                    var auth_name = document.getElementById("auth_name").value;
                    var auth_url = document.getElementById("auth_url").value;
                    var auth_desc = document.getElementById("auth_desc").value;
                    var is_active = $("input[name=is_active]:checked").val();
                    var auth_weight = document.getElementById("auth_weight").value;

                    $.ajax({
                        url: "{{urlfor "AuthController.DoAdd"}}",
                        type: "POST",
                        data: {
                            "auth_parent_id": auth_parent_id,
                            "auth_name": auth_name,
                            "auth_url": auth_url,
                            "auth_desc": auth_desc,
                            "is_active": is_active,
                            "auth_weight": auth_weight
                        },
                        success: function (data) {
                            var code = data["code"];
                            if(code == 200){
                                alert(data["msg"]);
                                var index = parent.layer.getFrameIndex(window.name);
                                //关闭当前frame
                                parent.layer.close(index);
                                window.location.href = "{{urlfor "AuthController.List"}}"
                                // ions_alert.alertSuccessToast(data["msg"]);
                                // setTimeout(reload_list,2000);
                            }else {
                                ions_alert.alertErrorToast(data["msg"]);
                                setTimeout(reload_list,20000);
                            }

                        },
                        fail: function (data) {
                            alert("添加数据失败，重新添加");
                        }
                    });


                    function reload_list() {
                        // 获得frame索引
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                        window.location.href = "{{urlfor "AuthController.List"}}"
                    }




                });


            });


        });

    </script>

  </body>

</html>